/**
 * Created by vicket on 2017/4/16.
 */
$(function(){

    //长按事件
    $.fn.longPress = function(fn) {
        var timeout = undefined;
        var $this = this;
        for(var i = 0;i<$this.length;i++){
            $this[i].addEventListener('touchstart', function(event) {
                var event=event;
                timeout = setTimeout(fn(event), 800);  //长按时间超过800ms，则执行传入的方法
            }, false);
            $this[i].addEventListener('touchend', function(event) {
                clearTimeout(timeout);  //长按时间少于800ms，不会执行传入的方法
            }, false);
        }
    }
    //设置删除图层高度
    var windowHeight=$(window).height();
    var deleteNovel=$(".delete_novel");
    deleteNovel.height(windowHeight)
    deleteNovel.css("display","none");
    var maskText=$(".mask_content p");
    //获取提醒弹出框
    var maskTip=$(".mask_tip");
    maskTip.height(windowHeight);
    maskTip.css("display","none");


    //获取书籍列表
    var bookList=$(".book_list");
    var useridVal=sessionStorage.getItem("userId");
    if(sessionStorage.getItem("username")){
        $.ajax({
            type:'get',
                url:"/api/frontGetBookcase",
            data:{userId:useridVal},
            success:function(result){
                var html=template("mybook",{"items":result});
                bookList.html(html);
                //删除功能
                var lis=$(".novelLi");
                lis.longPress(function(event){
                    event.preventDefault();
                    var bookId=event.target.parentNode.getAttribute("bookId");
                    deleteNovel.css("display","block");
                    var confirm=$(".confirm");
                    var cancel=$(".cancel");
                    confirm.on("touchstart",function(){
                        $.ajax({
                            type:"post",
                            url:"/api/deleteBookcase ",
                            data:{bId:bookId},
                            success:function(result){
                                deleteNovel.css("display","none");
                                maskText.text(result.message);
                                maskTip.css("display","block");
                                setTimeout(function(){
                                    location.reload();
                                },2000);

                            }
                        });
                    });
                    cancel.on("touchstart",function(){
                        deleteNovel.css("display","none");

                    })


                })
            }
        });
    }


})